package island

import (
	"github.com/Unixeno/islands/island/cgroups"
	"github.com/opencontainers/runtime-spec/specs-go"
	"path/filepath"
)

type CgroupsConfig struct {
	CgroupsPath    string                `json:"cgroupsPath,omitempty"` // cgroup path 必须是绝对路径
	LinuxResources *specs.LinuxResources `json:"resources,omitempty"`
}

const (
	cgroupsDir = "/islands"
)

func (container *base) SetCgroups(config CgroupsConfig) {
	if config.CgroupsPath == "" {
		config.CgroupsPath = filepath.Join(cgroupsDir, container.ID)
	}
	container.CgroupSetting = &config
}

func addCgroups(spec *specs.Spec, container *base) {
	if container.CgroupSetting == nil {
		return
	}
	spec.Linux.CgroupsPath = container.CgroupSetting.CgroupsPath
	spec.Linux.Resources = cgroups.Convert(container.CgroupSetting.LinuxResources, toolName)
}
